<?php
if (!defined('ZAZA')) exit('No direct script access allowed');
/**
 * @copyright     ZaZa Group
 * @author        Sir Doan
 */
class UserCurrent 
{
    public static $current = false;
    public $data = false; 
    /**
     * Start function
     */
    public function __construct($user = false) 
    {
        UserCurrent::$current = &$this;        
        if (!$user) {            
            if (isset($_SESSION [NAME_SESSION_USER] )) {
                $user = $_SESSION[NAME_SESSION_USER]; 
                UserCurrent::$current->data = $user;                    
                self::registerSession (NAME_SESSION_USER, $user ); 
                return;
            } else {
                UserCurrent::$current->data = false;
                return;
            }
        } 
        else 
        {
            UserCurrent::$current->data = $user;
            self::registerSession(NAME_SESSION_USER, $user );
        }        
    }
    
    /**
     * Register Session  
     * 
     * @param String $name
     * @param Array $value
     * @return Void
     */
    private static function registerSession($name, $value) 
    {                                
        $_SESSION [$name] = $value;
        $_SESSION ['session_key'][] = $name;         
    }
    
    /**
     * Clear Session
     * 
     * @return Void
     */
    private static function clearSession() {
        if (isset ( $_SESSION ['session_key'] ))
            foreach ( $_SESSION ['session_key'] as $key )
                unset ( $_SESSION [$key] );
    }
    /**
     * Check Login
     * 
     * @return Void
     */
    public static function isLogin() 
    {        
        return isset($_SESSION[NAME_SESSION_USER]);
    } 
    
    /**
     * Logout
     * 
     * @return Void
     */
    public static function logOut() 
    {
        self::clearSession();
    }  
}

// Initialize UserCurrent
UserCurrent::$current = new UserCurrent();
?>